package com.example.zsfinacialmanage.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import com.example.zsfinacialmanage.R;
import com.example.zsfinacialmanage.adapter.IncomeAdapter;
import com.example.zsfinacialmanage.bean.IncomeBean;
import com.example.zsfinacialmanage.db.MyDBHelper;

import java.util.ArrayList;
import java.util.List;

public class InComeDetailActivity extends AppCompatActivity {
    //1 定义对象
    RecyclerView recy_view;
    MyDBHelper mhelper;
    SQLiteDatabase db;
    List<IncomeBean> arr1=new ArrayList<IncomeBean>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_in_come_detail);
        //2 绑定控件
        initView();

        //3 准备数据
        initData();

        //4 设计每一行的子布局

        //5定义适配器： 数据和子布局关联起来（桥梁的作用）
        IncomeAdapter adapter=new IncomeAdapter(InComeDetailActivity.this,arr1);

        //6将适配器和布局管理器加载到控件当中
        StaggeredGridLayoutManager st=new StaggeredGridLayoutManager(StaggeredGridLayoutManager.VERTICAL,1);
        recy_view.setLayoutManager(st);
        recy_view.setAdapter(adapter);
    }

    //2 绑定控件----------------代码
    private void initView() {
        recy_view=findViewById(R.id.recy_view_indetail);
        mhelper=new MyDBHelper(InComeDetailActivity.this);
        db=mhelper.getWritableDatabase();
    }

    //3 准备数据----------------代码
    private void initData() {
    // 从数据库查询所有的新增收入信息,取出数据
    Cursor cursor = db.rawQuery("SELECT * FROM in_come", null);

    if (cursor != null && cursor.moveToFirst()) {
        do {
            int idColumnIndex = cursor.getColumnIndex("id");
            if (idColumnIndex == -1) {
                throw new IllegalArgumentException("Column 'id' not found in the table.");
            }
            int myid = cursor.getInt(idColumnIndex);

            int inmoneyColumnIndex = cursor.getColumnIndex("inmoney");
            if (inmoneyColumnIndex == -1) {
                throw new IllegalArgumentException("Column 'inmoney' not found in the table.");
            }
            double mymoney = cursor.getDouble(inmoneyColumnIndex);

            int intimeColumnIndex = cursor.getColumnIndex("intime");
            if (intimeColumnIndex == -1) {
                throw new IllegalArgumentException("Column 'intime' not found in the table.");
            }
            String mytime = cursor.getString(intimeColumnIndex);

            int intypeColumnIndex = cursor.getColumnIndex("intype");
            if (intypeColumnIndex == -1) {
                throw new IllegalArgumentException("Column 'intype' not found in the table.");
            }
            String mytype = cursor.getString(intypeColumnIndex);

            int inpayerColumnIndex = cursor.getColumnIndex("inpayer");
            if (inpayerColumnIndex == -1) {
                throw new IllegalArgumentException("Column 'inpayer' not found in the table.");
            }
            String mypayer = cursor.getString(inpayerColumnIndex);

            int inremarkColumnIndex = cursor.getColumnIndex("inremark");
            if (inremarkColumnIndex == -1) {
                throw new IllegalArgumentException("Column 'inremark' not found in the table.");
            }
            String myremark = cursor.getString(inremarkColumnIndex);

            IncomeBean incomeBean = new IncomeBean(myid, mymoney, mytime, mytype, mypayer, myremark);
            arr1.add(incomeBean);
        } while (cursor.moveToNext());
    }

    // 关闭游标
    if (cursor != null) {
        cursor.close();
    }
}


}